package stud.mpgame.flash.scene;

import flash.Vector;

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.StageAlign;

import flash.geom.Transform;

import flash.events.Event;

/**
 * ...
 * @author Artem Kirienko
 */
class PacmanReturns {
	
	private var _id:Int;
	private var player:Sprite;
	private var size:Int;
	private var sign:Int;
	private var mouthCoords:Vector<Float>;
	private var mouth:Array<Shape>;
	
	//public var id(getId, never):Int;
	
	public function new(x0:Float, y0:Float, nid:Int, world:GameWorldView) {
		
		_id = nid;
		sign = 1;


		player = new Sprite();
		
		player.graphics.beginFill(0xff9933, 1);
		player.graphics.drawCircle(0, 0, 20);
		player.graphics.endFill;
		mouthCoords = new Vector<Float>(6, true);
		mouthCoords[0] = 0;
		mouthCoords[1] = 0;
		mouthCoords[2] = 20;
		mouthCoords[4] = 20;
		mouth = new Array<Shape>();
		for (i in 0...11) {
			mouth.push(new Shape());
			mouth[i].graphics.beginFill(0xFFFFFF, 1);
			mouthCoords[3] = 12 - i;
			mouthCoords[5] = i - 12;
			mouth[i].x = mouth[i].y = 0;
			mouth[i].graphics.drawTriangles(mouthCoords);
			mouth[i].visible = false;
			player.addChild(mouth[i]);
			mouth[i].graphics.endFill();
		}


		mouth[0].visible = true;


		player.addEventListener(Event.ENTER_FRAME, onEnterFrame);


		player.x = x0;
		player.y = y0;
		world.addChild(player);
		size = 10;
	}
	
	public function onEnterFrame(e:Event)
	{
		if (size == 0)
		sign = -1;
		if (size == 10)
		sign = 1;		
		size -= sign;
		
		if (sign > 0) {
		mouth[size + 1].visible = false;
		}
		else {
		mouth[size - 1].visible = false;
		}
		mouth[size].visible = true;
	}
	
	public function remove() {
		player.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		player.parent.removeChild(player);
	}
	
	public function changeCoords(dx:Float, dy:Float) {
		player.x += dx;
		player.y += dy;
		var t:Float;
		t = 0;
		if (dx < 0)
		t = Math.PI;
		if (Std.int(dy) == 0) {
			if (dx < 0)
			player.rotation = 180;
            else
            player.rotation = 0;
		}
		else
		player.rotation = ((Math.atan(dy/dx) + t)/Math.PI)*180;
	}
	
	public function setCoords(x:Float, y:Float)
	{
		player.x = x;
		player.y = y;
	}
	
	public function getID():Int {
		return _id;
	}	
	
	public function getX():Float
	{
		return player.x;
	}
	
	public function getY():Float
	{
		return player.y;
	}
}